package com.example.stock.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.stock.eo.StockTradeEo;
import com.example.stock.mapper.StockTradeMapper;
import com.example.stock.vo.EmaTradeVo;
import com.example.stock.vo.common.TradeVo;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * @Author: zhuchunfeng
 * @Description
 * @Date: 2021/1/2 12:30
 * Copyright (c) 2019 北京新媒传信科技有限公司
 */
@Service
public class StockTradeService extends ServiceImpl<StockTradeMapper, StockTradeEo> {

    private static final BigDecimal CHARGE_RATE = new BigDecimal(0.00015);

    public BigDecimal doTrade(List<TradeVo> tradeVos, boolean save) {
        StockTradeEo tradeEo = new StockTradeEo()
                .setMoney(new BigDecimal(1000_000))
                .setHoldStatus((short) 0);

        for (TradeVo tradeVo : tradeVos) {
            BigDecimal price = tradeVo.getPrice();
            if (tradeVo.isCanBuy()) {
                if (emptyHold(tradeEo)) {
                    //可用金额(扣除手续费)
                    BigDecimal totalM = tradeEo.getMoney().subtract(tradeEo.getMoney().multiply(CHARGE_RATE))
                            .setScale(4, RoundingMode.HALF_UP);
                    tradeEo.setHoldStatus((short) 1);
                    tradeEo.setHoldTime(tradeVo.getTime());
                    tradeEo.setCharge(tradeEo.getMoney().multiply(new BigDecimal(0.00015)));
                    tradeEo.setHoldVol(totalM.divide(price, 4, RoundingMode.HALF_UP));
                    tradeEo.setMoney(totalM);
                    tradeEo.setPrice(price);
                    tradeEo.setProfit(BigDecimal.ZERO);

                    if (tradeVo instanceof EmaTradeVo) {
                        tradeEo.setExt1(((EmaTradeVo) tradeVo).getJj());
                    }
                    if (save) {
                        save(tradeEo);
                    }
                }

            } else {
                if (!emptyHold(tradeEo)) {
                    //当天进行过交易
                    if (tradeVo.getTime().getDayOfYear() == tradeEo.getHoldTime().getDayOfYear()) {
                        continue;
                    }
                    //金额 = 持仓*当前收盘价*(1-手续费)
                    BigDecimal money = tradeEo.getHoldVol().multiply(price).multiply(BigDecimal.ONE.subtract(CHARGE_RATE))
                            .setScale(4, RoundingMode.HALF_UP);
                    tradeEo.setProfit(money.subtract(tradeEo.getMoney()));
                    tradeEo.setMoney(money);
                    tradeEo.setCharge(tradeEo.getHoldVol().multiply(price).multiply(CHARGE_RATE)
                            .setScale(4, RoundingMode.HALF_UP));
                    tradeEo.setHoldVol(BigDecimal.ZERO);
                    tradeEo.setHoldStatus((short) 0);
                    tradeEo.setHoldTime(tradeVo.getTime());
                    tradeEo.setPrice(tradeVo.getPrice());
                    if (tradeVo instanceof EmaTradeVo) {
                        tradeEo.setExt1(((EmaTradeVo) tradeVo).getJj());
                    }
                    if (save) {
                        save(tradeEo);
                    }
                }
            }
        }
        return tradeEo.getMoney();
    }

    public boolean emptyHold(StockTradeEo hold) {
        return hold.getHoldStatus() == null || hold.getHoldStatus() == 0;
    }

}
